<?php

namespace app\home\controller\ump;

use app\home\controller\AuthController;
use app\home\model\system\SystemPoint;
use services\{FormBuilder as Form, UtilService as Util, JsonService};
use app\home\model\user\UserPoint as UserPointModel;
use think\facade\Route as Url;

/**
 * 用户的积分控制器
 * Class UserPoint
 * @package app\home\controller\ump
 */
class UserPoint extends AuthController
{
    /**
     * 积分首页
     * @return string
     */
    public function index()
    {
        $this->assign([
            'is_layui' => true,
            'year' => getMonth(),
        ]);
        return $this->fetch();
    }

    /**
     * 异步获取所有的
     * @return false|string
     */
    public function getponitlist()
    {
        $where = Util::getMore([
            ['start_time', ''],
            ['end_time', ''],
            ['nickname', ''],
            ['page', 1],
            ['limit', 10],
        ]);
        return JsonService::successlayui(UserPointModel::getpointlist($where));
    }

    /**
     * @return mixed
     */
    public function create()
    {
        $this->assign(['title' => '添加优惠券', 'action' => Url::buildUrl('save'), 'rules' => $this->rules()->getContent()]);
        return $this->fetch('public/common_form');
    }

    /**
     * 导出数据
     */
    public function export()
    {
        $where = Util::getMore([
            ['start_time', ''],
            ['end_time', ''],
            ['nickname', ''],
        ]);
        UserPointModel::SaveExport($where);
    }

    /**
     * 获取积分日志头部信息
     * @return false|string
     */
    public function getuserpointbadgelist()
    {
        $where = Util::getMore([
            ['start_time', ''],
            ['end_time', ''],
            ['nickname', ''],
        ]);
        return JsonService::successful(UserPointModel::getUserpointBadgelist($where));
    }

    /**
     * 积分套餐
     */
    public function meal()
    {
        $where = Util::getMore([
            ['is_show',''],
            ['title',''],
        ],$this->request);
        $this->assign(compact("where"));
        $this->assign(SystemPoint::systemPage($where));
        return $this->fetch();
    }

    /**
     * 删除
     * @param $id
     * @return false|string
     */
    public function deleteMeal($id=0)
    {
        if (!$id) return JsonService::fail("删除的套餐不存在");
        if (SystemPoint::where("id", $id)->update(["is_del" => 1]))
            return JsonService::successful("删除成功");
        return JsonService::fail("删除失败");
    }

    /**
     * 创建表单
     * @return string
     * @throws \FormBuilder\exception\FormBuilderException
     */
    public function createMeal()
    {
        $f = array();
        $f[] = Form::input('title','套餐名称');
        $f[] = Form::number('num','实际积分',0)->min(0);
        $f[] = Form::number('give','赠送的积分')->min(0);
        $f[] = Form::number('price','金额')->min(0);
        $f[] = Form::number('sort','排序');
        $f[] = Form::radio('is_show','是否显示',1)->options([['label'=>'开启','value'=>1],['label'=>'关闭','value'=>2]]);

        $form = Form::make_post_form('添加积分套餐',$f,Url::buildUrl('saveMeal'));
        $this->assign(compact('form'));
        return $this->fetch('public/form-builder');
    }

    public function saveMeal()
    {
        $data = Util::postMore([
            'title',
            'num',
            'give',
            'price',
            'sort',
            ['is_show',1]
        ]);
        if(!$data['title']) return JsonService::fail('请输入套餐名称');
        if(!$data['price']) return JsonService::fail('请输入套餐金额');
        $data['add_time'] = time();
        $data["wxapp_id"] = $this->wxapp_id;
        SystemPoint::create($data);
        return JsonService::successful('添加套餐!');
    }
}